package cn.stylefeng.guns.modular.knowledge.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.knowledge.entity.KnowledgeContent;
import cn.stylefeng.guns.modular.knowledge.mapper.KnowledgeContentMapper;
import cn.stylefeng.guns.modular.knowledge.model.params.KnowledgeContentParam;
import cn.stylefeng.guns.modular.knowledge.model.result.KnowledgeContentResult;
import  cn.stylefeng.guns.modular.knowledge.service.KnowledgeContentService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 知识点 服务实现类
 * </p>
 *
 * @author CHENCHEN
 * @since 2022-07-21
 */
@Service
public class KnowledgeContentServiceImpl extends ServiceImpl<KnowledgeContentMapper, KnowledgeContent> implements KnowledgeContentService {

    @Override
    public void add(KnowledgeContentParam param){
        KnowledgeContent entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(KnowledgeContentParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(KnowledgeContentParam param){
        KnowledgeContent oldEntity = getOldEntity(param);
        KnowledgeContent newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public KnowledgeContentResult findBySpec(KnowledgeContentParam param){
        return null;
    }

    @Override
    public List<KnowledgeContentResult> findListBySpec(KnowledgeContentParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(KnowledgeContentParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<Map<String, Object>> getKnowledgeList(Long specialtyId) {

        return this.baseMapper.getKnowledgeList(specialtyId);
    }

    @Override
    public KnowledgeContentResult selectDetailById(Long id) {

        return this.baseMapper.selectDetailById(id);
    }

    private Serializable getKey(KnowledgeContentParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private KnowledgeContent getOldEntity(KnowledgeContentParam param) {
        return this.getById(getKey(param));
    }

    private KnowledgeContent getEntity(KnowledgeContentParam param) {
        KnowledgeContent entity = new KnowledgeContent();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
